//
//  MainViewController.swift
//  我的微博
//
//  Created by 吴加将 on 15/9/6.
//  Copyright © 2015年 吴加将. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewController("首页",vc: HomeViewController(),imageName:"tabbar_home")
        addChildViewController("消息",vc: MessageViewController(),imageName:"tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController("发现",vc: DiscoverViewController(),imageName:"tabbar_discover")
        addChildViewController("我",vc: ProfileViewController(),imageName:"tabbar_profile")
        
        

        

           }
    //添加子控制器
    private func addChildViewController(title:String,vc:UIViewController,imageName:String){
        vc.title=title
       // tabBar.tintColor=UIColor.orangeColor()
        vc.tabBarItem.image=UIImage(named: imageName)
        let nvc=UINavigationController(rootViewController: vc)
        addChildViewController(nvc)
        }
    //按钮的点击事件
    func clickBtn(){
        print(__FUNCTION__)
    }
    //懒加载添加按钮
  private  lazy var composedbtn : UIButton = {
        let btn = UIButton()
    btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:UIControlState.Normal )
    btn.addTarget(self, action:"clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(btn)
    return btn
        
    }()
    //设置按钮的frame
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        setBtnFrame()
    }
        
      //设置按钮的frame
    func setBtnFrame(){
        let w = tabBar.bounds.width/CGFloat(childViewControllers.count)
        let f = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composedbtn.frame=CGRectOffset(f, w*2,0)
        
        
        
    }

}
